package com.dl7.mvp.module.video.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.dl7.mvp.R;
import com.dl7.mvp.adapter.RecommendAlbumsAdapter;
import com.dl7.mvp.injector.components.DaggerVideoListComponent;
import com.dl7.mvp.injector.modules.RecommendModule;
import com.dl7.mvp.local.table.VideoInfo;
import com.dl7.mvp.module.base.BaseFragment;
import com.dl7.mvp.module.base.IBasePresenter;
import com.dl7.mvp.module.base.ILoadDataView;
import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.helper.RecyclerViewHelper;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 首页推荐视频列表
 */
public class RecommendFragment extends BaseFragment<IBasePresenter> implements ILoadDataView<List<VideoInfo>> {

    private static final String VIDEO_LIST_ID = "VideoListId";

    RecyclerView mRvRecommendAlbumsList;

    @BindView(R.id.rv_recommend_video_list)
    RecyclerView mRvVideoList;

    @Inject
    BaseQuickAdapter mAdapter;

    RecommendAlbumsAdapter mGridAdapter;

    public static RecommendFragment newInstance() {
        return new RecommendFragment();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGridAdapter = new RecommendAlbumsAdapter(mContext);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_recommend_list;
    }

    @Override
    protected void initInjector() {
        DaggerVideoListComponent.builder()
                .applicationComponent(getAppComponent())
                .recommendModule(new RecommendModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initViews() {
        View headerView = LayoutInflater.from(mContext).inflate(R.layout.fragment_recommend_ablums_list, null);
        mRvRecommendAlbumsList = (RecyclerView) headerView.findViewById(R.id.rv_recommend_albums_list);
        mAdapter.addHeaderView(headerView);

        RecyclerViewHelper.initRecyclerViewV(mContext, mRvVideoList, mAdapter);
        RecyclerViewHelper.initRecyclerViewG(mContext, mRvRecommendAlbumsList, mGridAdapter, 4);
    }

    @Override
    protected void updateViews() {
        mPresenter.getData();
    }

    @Override
    public void loadData(List<VideoInfo> videoList) {
        mAdapter.updateItems(videoList);
        for (VideoInfo bean : videoList) {
            Log.e("RecommendFragment", bean.toString());
        }
    }

    @Override
    public void loadMoreData(List<VideoInfo> albumsList) {
        mGridAdapter.addItems(albumsList);
    }

    @Override
    public void loadNoData() {
        mAdapter.loadAbnormal();
    }
}
